using System.Runtime.InteropServices;

namespace Screenshot
{
    public partial class ScreenFrom : Form
    {
        public ScreenFrom()
        {
            InitializeComponent();
        }
        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
        public Point DownPoint;
        private bool CatchFinished = false;
        private bool CatchStart = false;
        private Bitmap originBmp;
        private Rectangle CatchRectangle;

        private void ScreenFrom_Load(object sender, EventArgs e)
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();
            this.Cursor = Cursors.Cross;
            originBmp = new Bitmap(this.BackgroundImage);
        }

        private void ScreenFrom_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void ScreenFrom_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!CatchStart)
                {
                    CatchStart = true;
                    DownPoint = new Point(e.X, e.Y);
                }
            }
        }

        private void ScreenFrom_MouseMove(object sender, MouseEventArgs e)
        {
            if (CatchStart)
            {
                using (Bitmap copyBmp = (Bitmap)originBmp.Clone())
                {
                    Point newPoint = new Point(DownPoint.X, DownPoint.Y);
                    Graphics g = Graphics.FromImage(copyBmp);
                    using (Pen p = new Pen(Color.Green,3))
                    {
                        int width = Math.Abs(e.X - DownPoint.X);
                        int height = Math.Abs(e.Y - DownPoint.Y);
                        if (e.X < DownPoint.X)
                        {
                            newPoint.X = e.X;
                        }
                        if (e.Y < DownPoint.Y)
                        {
                            newPoint.Y = e.Y;
                        }
                        CatchRectangle = new Rectangle(newPoint, new Size(width, height));
                        g.DrawRectangle(p, CatchRectangle);
                        CatchRectangle = new Rectangle(newPoint, new Size(width, height));
                        g.DrawRectangle(p, CatchRectangle);

                        // 添加文字显示尺寸
                        DrawSelectionDetails(g, CatchRectangle);
                    }
                    Graphics g1 = this.CreateGraphics();
                    g1.DrawImage(copyBmp, new Point(0, 0));
                    g1.Dispose();
                }
            }
        }
        private void DrawSelectionDetails(Graphics g, Rectangle rect)
        {
            using (SolidBrush brush = new SolidBrush(Color.Red))
            using (Font font = new Font("Arial", 15))
            {
                string sizeText = $"{rect.Width+1} * {rect.Height+1}";
                SizeF textSize = g.MeasureString(sizeText, font);
                Rectangle textRect = new Rectangle(rect.Location, new Size((int)textSize.Width, (int)textSize.Height));
                g.DrawString(sizeText, font, brush, textRect.Location);
            }

         
        }

        private void ScreenFrom_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (CatchStart)
                {
                    CatchStart = false;
                    CatchFinished = true;
                    ProcessCatchFinished();
                }
            }
        }

        private void ScreenFrom_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && CatchFinished)
            {
                ProcessCatchFinished();
            }
        }

        private void ProcessCatchFinished()
        {
            if (CatchFinished)
            {
                if (CatchRectangle.Height > 10 && CatchRectangle.Width > 10)
{
                using (Bitmap CatchedBmp = new Bitmap(CatchRectangle.Width, CatchRectangle.Height))
                {
                    using (Graphics g = Graphics.FromImage(CatchedBmp))
                    {
                        g.DrawImage(originBmp, new Rectangle(0, 0, CatchRectangle.Width, CatchRectangle.Height), CatchRectangle, GraphicsUnit.Pixel);
                        Clipboard.SetImage(CatchedBmp);
                    }
                    CatchFinished = false;
                    this.BackgroundImage = originBmp;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            }
        }
    }
}